/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yanzhenjie.andserver.dialog;

import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import com.yanzhenjie.hos.ResourceTable;

import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.miscservices.pasteboard.PasteData;
import ohos.miscservices.pasteboard.SystemPasteboard;
import ohos.utils.PacMap;

import java.util.ArrayList;

/**
 * Description: 仿知乎底部评论弹窗
 * Create by dance, at 2018/12/25
 */
public class ZhihuCommentPopup extends BottomPopupView {
    private String host;

    public ZhihuCommentPopup(Context context, String host) {
        super(context, null);
        this.host = host;
    }


    @Override
    protected int getImplLayoutId() {
        return ResourceTable.Layout_dialog;
    }

    private static final String ADDITON_KEY = "ADDITION_KEY";

    @Override
    protected void onCreate() {
        super.onCreate();

        Text address = (Text) findComponentById(ResourceTable.Id_address);
        Text copy = (Text) findComponentById(ResourceTable.Id_copy);
        //逻辑处理
        findComponentById(ResourceTable.Id_hide).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                dismiss();
            }
        });
        address.setText(host);
        copy.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                SystemPasteboard mPasteboard = SystemPasteboard.getSystemPasteboard(getContext());
                PasteData pasteData = new PasteData();
                pasteData.addTextRecord(host);
                PacMap pacMap = new PacMap();
                pacMap.putString(ADDITON_KEY, "ADDITION_VALUE_OF_TEXT");
                pasteData.getProperty().setAdditions(pacMap);
                pasteData.getProperty().setTag("USER_TAG");
                pasteData.getProperty().setLocalOnly(true);
                mPasteboard.setPasteData(pasteData);
                new ToastDialog(getContext()).setText("复制成功，请在浏览器中打开").show();
            }
        });
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .7f);
    }

}